﻿
namespace UserDefinedForms.Windows.Forms
{
	/// <summary>
	/// abstract ComponentSupplier typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class ComponentSupplierDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<ComponentSupplierDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.ComponentSupplier _currentComponentSupplier = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxComponentSupplierDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxComponentSupplierErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxComponentSupplierBindingSource;
		
		/// <summary> 
		/// the DGV column associated with the ControlId property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxControlIdDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the SupplierId property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxSupplierIdDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.FormControl> _ControlIdList;
		
		/// <summary> 
		/// The list of selectable FormControl
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.FormControl> ControlIdList
		{
			get {return this._ControlIdList;}
			set 
			{
				this._ControlIdList = value;
				this.uxControlIdDataGridViewColumn.DataSource = null;
				this.uxControlIdDataGridViewColumn.DataSource = this._ControlIdList;
			}
		}
		
		private bool _allowNewItemInControlIdList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of FormControl
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInControlIdList
		{
			get { return _allowNewItemInControlIdList;}
			set
			{
				this._allowNewItemInControlIdList = value;
				this.uxControlIdDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
				
		private Entities.TList<Entities.Supplier> _SupplierIdList;
		
		/// <summary> 
		/// The list of selectable Supplier
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.Supplier> SupplierIdList
		{
			get {return this._SupplierIdList;}
			set 
			{
				this._SupplierIdList = value;
				this.uxSupplierIdDataGridViewColumn.DataSource = null;
				this.uxSupplierIdDataGridViewColumn.DataSource = this._SupplierIdList;
			}
		}
		
		private bool _allowNewItemInSupplierIdList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of Supplier
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInSupplierIdList
		{
			get { return _allowNewItemInSupplierIdList;}
			set
			{
				this._allowNewItemInSupplierIdList = value;
				this.uxSupplierIdDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.ComponentSupplier> _ComponentSupplierList;
				
		/// <summary> 
		/// The list of ComponentSupplier to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.ComponentSupplier> ComponentSupplierList
		{
			get {return this._ComponentSupplierList;}
			set
			{
				this._ComponentSupplierList = value;
				this.uxComponentSupplierBindingSource.DataSource = null;
				this.uxComponentSupplierBindingSource.DataSource = value;
				this.uxComponentSupplierDataGridView.DataSource = null;
				this.uxComponentSupplierDataGridView.DataSource = this.uxComponentSupplierBindingSource;				
				//this.uxComponentSupplierBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxComponentSupplierBindingSource_ListChanged);
				this.uxComponentSupplierBindingSource.CurrentItemChanged += new System.EventHandler(OnComponentSupplierBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnComponentSupplierBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentComponentSupplier = uxComponentSupplierBindingSource.Current as Entities.ComponentSupplier;
			
			if (_currentComponentSupplier != null)
			{
				_currentComponentSupplier.Validate();
			}
			//_ComponentSupplier.Validate();
			OnCurrentEntityChanged();
		}

		//void uxComponentSupplierBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <see cref="Entities.ComponentSupplier"/> instance.
		/// </summary>
		public Entities.ComponentSupplier SelectedComponentSupplier
		{
			get {return this._currentComponentSupplier;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxComponentSupplierDataGridView.VirtualMode;}
			set
			{
				this.uxComponentSupplierDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxComponentSupplierDataGridView.AllowUserToAddRows;}
			set {this.uxComponentSupplierDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxComponentSupplierDataGridView.AllowUserToDeleteRows;}
			set {this.uxComponentSupplierDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <see cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxComponentSupplierDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxComponentSupplierDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="ComponentSupplierDataGridViewBase"/> class.
		/// </summary>
		public ComponentSupplierDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxComponentSupplierDataGridView = new System.Windows.Forms.DataGridView();
			this.uxComponentSupplierBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxComponentSupplierErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxControlIdDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxSupplierIdDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			//this.uxControlIdBindingSource = new FormControlBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxControlIdBindingSource)).BeginInit();
			//this.uxSupplierIdBindingSource = new SupplierBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxSupplierIdBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxComponentSupplierDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxComponentSupplierBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxComponentSupplierErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxComponentSupplierErrorProvider
			// 
			this.uxComponentSupplierErrorProvider.ContainerControl = this;
			this.uxComponentSupplierErrorProvider.DataSource = this.uxComponentSupplierBindingSource;						
			// 
			// uxComponentSupplierDataGridView
			// 
			this.uxComponentSupplierDataGridView.AutoGenerateColumns = false;
			this.uxComponentSupplierDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxComponentSupplierDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxControlIdDataGridViewColumn,
		this.uxSupplierIdDataGridViewColumn			});
			this.uxComponentSupplierDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxComponentSupplierDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxComponentSupplierDataGridView.Name = "uxComponentSupplierDataGridView";
			this.uxComponentSupplierDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxComponentSupplierDataGridView.TabIndex = 0;	
			this.uxComponentSupplierDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxComponentSupplierDataGridView.EnableHeadersVisualStyles = false;
			this.uxComponentSupplierDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnComponentSupplierDataGridViewDataError);
			this.uxComponentSupplierDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnComponentSupplierDataGridViewCellValueNeeded);
			this.uxComponentSupplierDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnComponentSupplierDataGridViewCellValuePushed);
			
			//
			// uxControlIdDataGridViewColumn
			//
			this.uxControlIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxControlIdDataGridViewColumn.DataPropertyName = "ControlId";
			this.uxControlIdDataGridViewColumn.HeaderText = "ControlId";
			this.uxControlIdDataGridViewColumn.Name = "uxControlIdDataGridViewColumn";
			this.uxControlIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxControlIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxControlIdDataGridViewColumn.ReadOnly = false;		
			//
			// uxSupplierIdDataGridViewColumn
			//
			this.uxSupplierIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxSupplierIdDataGridViewColumn.DataPropertyName = "SupplierId";
			this.uxSupplierIdDataGridViewColumn.HeaderText = "SupplierId";
			this.uxSupplierIdDataGridViewColumn.Name = "uxSupplierIdDataGridViewColumn";
			this.uxSupplierIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxSupplierIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxSupplierIdDataGridViewColumn.ReadOnly = false;		
			//
			// uxControlIdDataGridViewColumn
			//				
			this.uxControlIdDataGridViewColumn.DisplayMember = "Name";	
			this.uxControlIdDataGridViewColumn.ValueMember = "ControlId";	
			this.uxControlIdDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxControlIdDataGridViewColumn.DataSource = uxControlIdBindingSource;				
				
			//
			// uxSupplierIdDataGridViewColumn
			//				
			this.uxSupplierIdDataGridViewColumn.DisplayMember = "SupplierName";	
			this.uxSupplierIdDataGridViewColumn.ValueMember = "SupplierId";	
			this.uxSupplierIdDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxSupplierIdDataGridViewColumn.DataSource = uxSupplierIdBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxComponentSupplierDataGridView);
			this.Name = "ComponentSupplierDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxControlIdBindingSource)).EndInit();
			//((System.ComponentModel.ISupportInitialize)(this.uxSupplierIdBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxComponentSupplierErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxComponentSupplierDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxComponentSupplierBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				ComponentSupplierDataGridViewEventArgs args = new ComponentSupplierDataGridViewEventArgs();
				args.ComponentSupplier = _currentComponentSupplier;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class ComponentSupplierDataGridViewEventArgs : System.EventArgs
		{
			private Entities.ComponentSupplier	_ComponentSupplier;
	
			/// <summary>
			/// the  Entities.ComponentSupplier instance.
			/// </summary>
			public Entities.ComponentSupplier ComponentSupplier
			{
				get { return _ComponentSupplier; }
				set { _ComponentSupplier = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxComponentSupplierDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnComponentSupplierDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxComponentSupplierDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnComponentSupplierDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxComponentSupplierDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxControlIdDataGridViewColumn":
						e.Value = ComponentSupplierList[e.RowIndex].ControlId;
						break;
					case "uxSupplierIdDataGridViewColumn":
						e.Value = ComponentSupplierList[e.RowIndex].SupplierId;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxComponentSupplierDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnComponentSupplierDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxComponentSupplierDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxControlIdDataGridViewColumn":
						ComponentSupplierList[e.RowIndex].ControlId = (System.Int32)e.Value;
						break;
					case "uxSupplierIdDataGridViewColumn":
						ComponentSupplierList[e.RowIndex].SupplierId = (System.Int32)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
